package org.example.niceoil.controller;
import org.example.niceoil.entity.teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.niceoil.dao.teacherDao;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher") // 设置统一的路径前缀
public class teacherController {

    private final teacherDao teacherDao;

    @Autowired
    public teacherController(teacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    // 查询所有教师
    @PostMapping("/all")
    public List<teacher> selectAll() {
        return teacherDao.selectAll();
    }

    // 根据姓名查询教师
    @PostMapping("/byName")
    public List<teacher> selectByName(@RequestBody Map<String, String> payload) {
        String name = payload.get("name");
        System.out.println(name + "===========");
        return List.of(teacherDao.selectByName(name));
    }
 @PostMapping("/bynameid")
    public List<teacher> selectByNameAndId(@RequestBody Map<String, String> payload) {
        String name = payload.get("name");
     String str = payload.get("id");
     if(str.isEmpty()){
         return null;
     }
     Long id =  Long.parseLong(str);
     System.out.println(payload);
     System.out.println("======================");
     if(name.equals("")){
         return null;
     }
        return List.of(teacherDao.selectByNameAndId(name,id));
    }

    // 查询 ID 最大的教师
    @GetMapping("/max")
    public teacher selectMax() {
        return teacherDao.selectMax();
    }

    // 新增教师
    @PostMapping("/add")
    public String addteacher(@RequestBody Map<String, Object> payload) {
        String str = (String) payload.get("id");
        Long id =  Long.parseLong(str);  // 安全地转换为 long 类型
        String name = (String) payload.get("name");
        int result = teacherDao.insertteacher(id, name);
        return result > 0 ? "teacher added successfully!" : "Failed to add teacher.";
    }

    // 删除教师
    @DeleteMapping("/delete")
    public String deleteteacher(@RequestBody Map<String, Object> payload) {
        Object idObject = payload.get("id");

        Long id;
        if (idObject instanceof String) {
            // 如果 id 是字符串，直接解析为 Long
            id = Long.parseLong((String) idObject);
        } else if (idObject instanceof Number) {
            // 如果 id 是数字，直接获取其 long 值
            id = ((Number) idObject).longValue();
        } else {
            // 如果类型不符合预期，抛出异常
            throw new IllegalArgumentException("Invalid type for 'id'. Expected String or Number.");
        }
        int result = teacherDao.deleteteacher(id);
        return result > 0 ? "teacher deleted successfully!" : "Failed to delete teacher.";
    }

    // 修改教师信息
    @PutMapping("/update")
    public String updateteacher(@RequestBody Map<String, Object> payload) {
        Object idObject = payload.get("id");

        Long id;
        if (idObject instanceof String) {
            // 如果 id 是字符串，直接解析为 Long
            id = Long.parseLong((String) idObject);
        } else if (idObject instanceof Number) {
            // 如果 id 是数字，直接获取其 long 值
            id = ((Number) idObject).longValue();
        } else {
            // 如果类型不符合预期，抛出异常
            throw new IllegalArgumentException("Invalid type for 'id'. Expected String or Number.");
        }
        String name = (String) payload.get("name");
        int result = teacherDao.updateteacher(id, name);
        return result > 0 ? "teacher updated successfully!" : "Failed to update teacher.";
    }
}

